﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports Utilerias
Imports WSFactory
Imports Npgsql
Imports MySql.Data.MySqlClient

Public Class EmpresaDAL

    Private cadConex As String = ""

    Public Sub New(ByVal pCadConex As String)
        Me.cadConex = pCadConex
    End Sub

#Region "FuncionesUtilizables"
    Public Function actualizaDatosFiscales(ByVal pEmpresa As empresaBE) As Boolean
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "UPDATE empresa " & _
                                                    "SET RazonSocial = @RazonSocial, " & _
                                                        "DF_Calle = @DF_Calle, " & _
                                                        "DF_NumExt = @DF_NumExt, " & _
                                                        "DF_NumInt = @DF_NumInt, " & _
                                                        "DF_Colonia = @DF_Colonia, " & _
                                                        "DF_Localidad = @DF_Localidad, " & _
                                                        "DF_Referencia = @DF_Referencia, " & _
                                                        "DF_Municipio = @DF_Municipio, " & _
                                                        "DF_Estado = @DF_Estado, " & _
                                                        "DF_Pais = @DF_Pais, " & _
                                                        "DF_CP = @DF_CP, " & _
                                                        "Sector = @Sector, " & _
                                                        "Telefono = @Telefono, " & _
                                                        "Correo = @Correo, " & _
                                                        "CvePemex = @CvePemex " & _
                                                    "WHERE RFC = @RFC;"
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Clear()
                    myComm.Parameters.AddWithValue("@RazonSocial", pEmpresa.RazonSocial)
                    myComm.Parameters.AddWithValue("@DF_Calle", pEmpresa.DF_Calle)
                    myComm.Parameters.AddWithValue("@DF_NumExt", pEmpresa.DF_NumExt)
                    myComm.Parameters.AddWithValue("@DF_NumInt", pEmpresa.DF_NumInt)
                    myComm.Parameters.AddWithValue("@DF_Colonia", pEmpresa.DF_Colonia)
                    myComm.Parameters.AddWithValue("@DF_Localidad", pEmpresa.DF_Localidad)
                    myComm.Parameters.AddWithValue("@DF_Referencia", pEmpresa.DF_Referencia)
                    myComm.Parameters.AddWithValue("@DF_Municipio", pEmpresa.DF_Municipio)
                    myComm.Parameters.AddWithValue("@DF_Estado", pEmpresa.DF_Estado)
                    myComm.Parameters.AddWithValue("@DF_Pais", pEmpresa.DF_Pais)
                    myComm.Parameters.AddWithValue("@DF_CP", pEmpresa.DF_CP)
                    myComm.Parameters.AddWithValue("@Sector", pEmpresa.Sector)
                    myComm.Parameters.AddWithValue("@Telefono", pEmpresa.Telefono)
                    myComm.Parameters.AddWithValue("@Correo", pEmpresa.Correo)
                    myComm.Parameters.AddWithValue("@IdEmpresa", pEmpresa.IdEmpresa)
                    myComm.Parameters.AddWithValue("@CvePemex", pEmpresa.ClavePemex)
                    myComm.Parameters.AddWithValue("@RFC", pEmpresa.RFC)
                    myConn.Open()
                    If (myComm.ExecuteNonQuery() = 1) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function getEmpresa(ByVal Rfc As String) As empresaBE
        Dim tmpEmpresa As empresaBE
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "SELECT IdEmpresa " & _
                                            "    , RFC " & _
                                            "    , RazonSocial " & _
                                            "    , DF_Calle " & _
                                            "    , DF_NumExt " & _
                                            "    , DF_NumInt " & _
                                            "    , DF_Colonia " & _
                                            "    , DF_Localidad " & _
                                            "    , DF_Referencia " & _
                                            "    , DF_Municipio " & _
                                            "    , DF_Estado " & _
                                            "    , DF_Pais " & _
                                            "    , DF_CP " & _
                                            "    , Sector " & _
                                            "    , Telefono " & _
                                            "    , Correo " & _
                                            "    , CvePemex " & _
                                            "FROM Empresa " & _
                                            "WHERE RFC = @Rfc;"
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddWithValue("@Rfc", Rfc)
                    myConn.Open()
                    Using myDR As MySqlDataReader = myComm.ExecuteReader()
                        If (myDR.HasRows()) Then
                            myDR.Read()
                            tmpEmpresa = EmpresaFact.Build(myDR)
                        Else
                            tmpEmpresa = Nothing
                        End If
                    End Using
                End Using
            End Using
            Return tmpEmpresa

        Catch ex As Exception
            Throw
        Finally
            tmpEmpresa = Nothing
        End Try
    End Function

    Function getRFCEmpresa() As String
        Dim objRfc As Object
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "SELECT RFC " & _
                                            "FROM Empresa ;"
                    myComm.CommandType = CommandType.Text
                    myConn.Open()
                    objRfc = myComm.ExecuteScalar()
                    If (objRfc Is Nothing) Then
                        Return ""
                    Else
                        Return objRfc.ToString()
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        Finally
            objRfc = Nothing
        End Try
    End Function
#End Region

    


End Class
